package jp.isisredirect.DreamWaverFirst;

import jp.isisredirect.DreamWaverFirst.Slider.SliderPositionListener;

import android.content.res.Resources;
/*import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.style.RelativeSizeSpan;
import android.text.style.SubscriptSpan;
*/
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class FreqController {
	private int mHeadChNumber;
	private DreamWaverFirstActivity mActivity;

	private int getFreqFromSliderVal(double value) {
		int f = (int) (value * 30);
		return f;
	}
	private int getIntervalTimeFromFreq(int f) {
		try {
			int v = (int) (1000. / f);
			return v;
		}catch(ArithmeticException e){
			return Integer.MAX_VALUE;
		}
	}

	class FreqValueUpdater implements Slider.SliderPositionListener {
		private TextView mTarget;
		private final byte mCommandTarget;
		private int prevVal;
		private long prevtime;

		FreqValueUpdater(TextView target, int colorIndex) {
			mTarget = target;
			mCommandTarget = (byte) (mHeadChNumber + colorIndex);
			prevVal = 5;
			prevtime = 0;
		}

		public void onPositionChange(double value) {
			long currentTimeMillis = System.currentTimeMillis();
			if (300 < (currentTimeMillis - prevtime)) {
				prevtime = currentTimeMillis;
				int f = getFreqFromSliderVal(value);
				int v = getIntervalTimeFromFreq(f);
				if (prevVal != v) {
					mTarget.setText(String.valueOf(f));
					if (mActivity != null) {
						mActivity.makeFreqCommand(mCommandTarget, v, 0);
					}
					prevVal = v;
				}
			}
		}

	}

	class LabelClickListener implements OnClickListener {
		final private double mValue;
		private final Slider mSlider;

		public LabelClickListener(Slider slider, double value) {
			mSlider = slider;
			mValue = value;
		}

		public void onClick(View v) {
			mSlider.setPosition(mValue);
		}

	}

	public FreqController(DreamWaverFirstActivity activity, int number, Resources res, boolean vertical) {
		mActivity = activity;
		mHeadChNumber = number;
	}

	public void attachToView(ViewGroup targetView) {
		for (int i = 0; i < 6; ++i) {
			ViewGroup g = (ViewGroup) targetView.getChildAt(i);

			TextView label = (TextView) g.getChildAt(0);
			Slider slider = (Slider) g.getChildAt(1);
			TextView valueText = (TextView) g.getChildAt(2);

			SliderPositionListener positionListener = new FreqValueUpdater(valueText, i);
			slider.setPositionListener(positionListener);
			LabelClickListener leftLabelListener = new LabelClickListener(slider, 0);
			label.setOnClickListener(leftLabelListener);
			LabelClickListener rightLabelListener = new LabelClickListener(slider, 1);
			valueText.setOnClickListener(rightLabelListener);

			valueText.setText("0");
		}
	}
	
	public void	sendAll(ViewGroup targetView) {
		for (int i = 0; i < 6; ++i) {
			ViewGroup g = (ViewGroup) targetView.getChildAt(i);

			Slider slider = (Slider) g.getChildAt(1);
			double pos = slider.getPosition();
			int f = getFreqFromSliderVal(pos);
			int v = getIntervalTimeFromFreq(f);
			if (mActivity != null) {
				mActivity.makeFreqCommand(mHeadChNumber+i, v, 0);
			}
		}
	}
}
